//
//  BK_OD_RedDot_View.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/3/3.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_OD_RedDot_View.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"
@implementation BK_OD_RedDot_View
{
    UILabel *_redDot;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    CGSize oneWordSize = [@"9" sizeWithAttributes:@{NSFontAttributeName : UIFontCustom(9)}];
    
    _redDot = [[UILabel alloc] init];
    _redDot.font = UIFontCustom(9);
    _redDot.textColor = kOrderWhite;
    _redDot.textAlignment = NSTextAlignmentCenter;
    _redDot.layer.cornerRadius = (oneWordSize.height + 4) / 2.0;
    _redDot.layer.masksToBounds = YES;
    [self addSubview:_redDot];
    
    [_redDot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self);
        make.height.mas_equalTo(oneWordSize.height + 4);
        make.width.mas_equalTo(oneWordSize.height + 4);
    }];
}

- (void)setCount:(NSInteger)count
{
    _redDot.backgroundColor = [UIColor colorWithRed:1.00 green:0.06 blue:0.09 alpha:1.00];
    NSString *text;
    if (count < 100) {
        text = [NSString stringWithFormat:@"%ld", (long)count];
    }
    else{
        text = @"99+";
    }
    _redDot.text = text;
    
    CGSize oneWordSize = [text sizeWithAttributes:@{NSFontAttributeName : UIFontCustom(9)}];
    CGFloat width = oneWordSize.height > oneWordSize.width ? oneWordSize.height : oneWordSize.width;
    [_redDot mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width + 4);
    }];
}

@end


